package ttelectronic.mobileholder.core;

import ttelectronic.mobileholder.R;
import ttelectronic.mobileholder.common.MHCommon;
import ttelectronic.mobileholder.dataaccess.MHDataBaseWrapper;
import ttelectronic.mobileholder.dataaccess.MHPinObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MHPinSettingActivity extends Activity {

	private TextView tvPinLevel;
	private TextView tvTemLevel;
	private SeekBar sbPinLevel;
	private SeekBar sbTemLevel;
	private int tmp;
	private MHDataBaseWrapper database;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pin_setting);

		tvPinLevel = (TextView) findViewById(R.id.tvPinLevel);
		tvPinLevel.setText(MHCommon.PIN_CONFIGURATION + "");
		tvTemLevel = (TextView) findViewById(R.id.tvTemperatureLevel);
		tvTemLevel.setText(MHCommon.TEM_CONFIGURATION + "");

		sbPinLevel = (SeekBar) findViewById(R.id.sbPinLevel);
		sbPinLevel.setProgress(MHCommon.PIN_CONFIGURATION);
		sbPinLevel.setOnSeekBarChangeListener(onPinChange);

		sbTemLevel = (SeekBar) findViewById(R.id.sbTemLevel);
		sbTemLevel.setProgress(MHCommon.TEM_CONFIGURATION);
		sbTemLevel.setOnSeekBarChangeListener(onTemChange);

		database = new MHDataBaseWrapper(this);
	}

	OnSeekBarChangeListener onPinChange = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			synchronized (this) {
				MHCommon.PIN_CONFIGURATION = seekBar.getProgress();
				database.updateBook(new MHPinObject(1,
						MHCommon.PIN_CONFIGURATION, MHCommon.TEM_CONFIGURATION));
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			tvPinLevel.setText(progress + "");
		}
	};

	OnSeekBarChangeListener onTemChange = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			synchronized (this) {
				MHCommon.TEM_CONFIGURATION = seekBar.getProgress();
				database.updateBook(new MHPinObject(1,
						MHCommon.PIN_CONFIGURATION, MHCommon.TEM_CONFIGURATION));
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			tvTemLevel.setText(progress + "");
			tmp = progress;
		}
	};
}
